package com.weifly.jettydoc.compare;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML.Tag;
import javax.swing.text.html.parser.ParserDelegator;

/**
 * 比较原文和译文的区别
 * 
 * @author weijx
 * @date 2011-06-17
 */
public class ContentComparer {

	public static String aptFile = "D:\\work\\eclipse\\eclipse3_5_1_jetty\\workspace\\jettyDoc\\src\\site\\apt\\jetty.apt";
	
	public static String htmlFile = "D:\\work\\eclipse\\eclipse3_5_1_jetty\\workspace\\jettyDoc\\target\\site\\jetty.html";
	
	public static void main(String[] args) throws Exception {
		new ContentComparer().compareFile(aptFile, htmlFile);
	}
	
	private ParserDelegator parser = new ParserDelegator();
	
	private String url;
	
	private int line;
	
	private List tagList;
	
	public void compareFile(String apt, String html) throws Exception{
		String wikiURL = findWikiURL(apt);
		if(wikiURL==null){
			return ;
		}
		
		// 读取wiki页面
		url = wikiURL;
		line = 0;
		List wikiTagList = tagList = new ArrayList();
		URL wiki = new URL(wikiURL);
		URLConnection con = wiki.openConnection();
		con.connect();
		readFromInput(con.getInputStream(), "UTF-8");
		con.getInputStream().close();
		
		// 读取html页面
		File hf = new File(html);
		url = hf.getName();
		line = 0;
		List htmlTagList = tagList = new ArrayList();
		FileInputStream fileInput = new FileInputStream(hf);
		readFromInput(fileInput, "UTF-8");
		fileInput.close();
		
		System.out.println("check wiki tag ------------------------");
		// 查出wiki中特有的连接
		for(int i=0;i<wikiTagList.size();i++){
			Object[] wikiTag = (Object[])wikiTagList.get(i);
			String a1 = (String)wikiTag[3];
			if(a1.startsWith("/Jetty")){
				a1 = "." + a1;
				if(a1.indexOf(".")==-1){
					a1 = a1+".html";
				}
				boolean find = false;
				for(int j=0;j<htmlTagList.size();j++){
					Object[] htmlTag = (Object[])htmlTagList.get(j);
					String a2 = (String)htmlTag[3];
					if(a1.equals(a2)){
						find=true;
					}
				}
				if(!find){
					outTag(wikiTag);
				}
			}
		}
	}
	
	private String findWikiURL(String apt){
		BufferedReader br = null;
		try {
			FileReader fr = new FileReader(apt);
			br = new BufferedReader(fr);
			String str = null;
			while((str=br.readLine())!=null){
				if(!str.startsWith("~~")){
					break;
				}
				str = str.substring(2).trim();
				if(str.startsWith("original")){
					int pos = str.indexOf(":");
					return str.substring(pos+1).trim();
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {}
		}
		return null;
	}
	
	private void readFromInput(InputStream input, String charset) throws Exception{
		InputStreamReader reader = new InputStreamReader(input, charset);
		parser.parse(reader, new HtmlParserListener(), true);
	}
	
	private void handleATag(Tag t, MutableAttributeSet a, int pos){
		String name = t.toString();
		// 不是a标签则返回
		if(!"a".equalsIgnoreCase(name)){
			return ;
		}
		Object hrefKey = HTML.getAttributeKey("href");
		Object hrefObj = a.getAttribute(hrefKey);
		// href为空则返回
		if(hrefObj==null){
			return ;
		}
		String href = hrefObj.toString();
		if(href==null || href.trim().length()==0){
			return ;
		}
		
		// 是绝对地址则返回
		if(href.startsWith("http://") || href.startsWith("https://")){
			return ;
		}
		
		// 是锚点则返回
		if(href.startsWith("#")){
			return ;
		}
		
		// 打印不存在的页面
		
		Object[] item = new Object[4];
		item[0] = url;
		item[1] = line;
		item[2] = pos;
		item[3] = href;
		tagList.add(item);
		outTag(item);
	}
	
	private void outTag(Object[] item){
		System.out.println(item[0] + " : " + " [" + item[1] + "," + item[2] + "] " + " href=" + item[3]);
	}
	
	private class HtmlParserListener extends HTMLEditorKit.ParserCallback{

		public void handleEndOfLineString(String eol) {
			line++;
		}

		public void handleEndTag(Tag t, int pos) {
			
		}

		public void handleError(String errorMsg, int pos) {
			
		}

		public void handleSimpleTag(Tag t, MutableAttributeSet a, int pos) {
			handleATag(t, a, pos);
		}

		public void handleStartTag(Tag t, MutableAttributeSet a, int pos) {
			handleATag(t, a, pos);
		}

	}
}
